(function($, window, document, undefined) {
    //定义分页类
    function Paging(element, options) {
        this.element = element;
        //传入形参
        this.options = {
            pageNo: options.pageNo||1,
            totalPage: options.totalPage,
            totalSize:options.totalSize,
            callback:options.callback
        };
        //根据形参初始化分页html和css代码
        this.init();
    }
    //对Paging的实例对象添加公共的属性和方法
    Paging.prototype = {
        constructor: Paging,
        init: function() {
            this.creatHtml($("#paging_js_page_size").val());
            this.bindEvent();
        },
        creatHtml: function(pageSize) {
            var me = this;

            var content = "";
            // content += "<li>";
            content += "<button onclick='table2excel(\"table2excel_table\")' id='paging_js_export' class='btn btn-sm btn-default hidden-xs' style='width:60px;display: inline-block;float:right;height: 28px;margin-left: 10px;'>导出</button>";
            content += "<select id='paging_js_page_size' class='form-control hidden-xs' style='width:60px;display: inline-block;float:right;height: 28px;margin-left: 10px;padding:0px;'>";
            if(pageSize && pageSize!= "" ){
                if(pageSize == 10){
                    content += "<option value='10' selected>10</option>";
                }else{
                    content += "<option value='10'>10</option>";
                }
                if(pageSize == 50){
                    content += "<option value='50' selected>50</option>";
                }else{
                    content += "<option value='50'>50</option>";
                }
                if(pageSize == 100){
                    content += "<option value='100' selected>100</option>";
                }else{
                    content += "<option value='100'>100</option>";
                }
                if(pageSize == 1000){
                    content += "<option value='1000' selected>1000</option>";
                }else{
                    content += "<option value='1000'>1000</option>";
                }
                if(pageSize == 1000000000){
                    content += "<option value='1000000000' selected>全部</option>";
                }else{
                    content += "<option value='1000000000'>全部</option>";
                }
            }else{
                content += "<option value='10'>10</option>";
                content += "<option value='50'>50</option>";
                content += "<option value='100'>100</option>";
                content += "<option value='1000'>1000</option>";
                content += "<option value='1000000000'>全部</option>";
            }
            content += "</select>";

            var current = me.options.pageNo;
            var total = me.options.totalPage;
            var totalNum = me.options.totalSize;

            content += "<ul class=\"pagination pagination-sm m-t-none m-b-none\" style='float:right;'>";

            content += "<li><a href=\"javascript:;\">首页</a></li>";
            content += "<li><a href=\"javascript:;\">上一页</a></li>";
            // content += "<a id=\"firstPage\">首页</a><a id='prePage'>上一页</a>";
            //总页数大于6时候
            if(total > 6) {
                //当前页数小于5时显示省略号
                if(current < 5) {
                    for(var i = 1; i < 6; i++) {
                        if(current == i) {
                            content += "<li><a href=\"javascript:;\" class='current' style='color:#fff;background-color: #428bca;'>" + i + "</a></li>";
                        } else {
                            content += "<li class='hidden-xs'><a href=\"javascript:;\">" + i + "</a></li>";
                        }
                    }
                    content += "<li class='hidden-xs'><div style=\"float: left;padding-left:5px;padding-right: 5px;\">. . .</div></li>";
                    content += "<li class='hidden-xs'><a href=\"javascript:;\">"+total+"</a></li>";
                } else {
                    //判断页码在末尾的时候
                    if(current < total - 3) {
                        for(var i = current - 2; i < current + 3; i++) {
                            if(current == i) {
                                content += "<li><a href=\"javascript:;\" class='current' style='color:#fff;background-color: #428bca;'>" + i + "</a></li>";
                            } else {
                                content += "<li class='hidden-xs'><a href=\"javascript:;\">" + i + "</a></li>";
                            }
                        }
                        content += "<li class='hidden-xs'><div style=\"float: left;padding-left:5px;padding-right: 5px;\">. . .</div></li>";
                        content += "<li class='hidden-xs'><a href=\"javascript:;\">"+total+"</a></li>";
                        //页码在中间部分时候
                    } else {
                        content += "<li class='hidden-xs'><a href=\"javascript:;\">1</a></li>";
                        content += "<li class='hidden-xs'><div style=\"float: left;padding-left:5px;padding-right: 5px;\">. . .</div></li>";
                        for(var i = total - 4; i < total + 1; i++) {
                            if(current == i) {
                                content += "<li><a href=\"javascript:;\" class='current' style='color:#fff;background-color: #428bca;'>" + i + "</a></li>";
                            } else {
                                content += "<li class='hidden-xs'><a href=\"javascript:;\">" + i + "</a></li>";
                            }
                        }
                    }
                }
                //页面总数小于6的时候
            } else {
                for(var i = 1; i < total + 1; i++) {
                    if(current == i) {
                        content += "<li><a href=\"javascript:;\" class='current' style='color:#fff;background-color: #428bca;'>" + i + "</a></li>";
                    } else {
                        content += "<li class='hidden-xs'><a href=\"javascript:;\">" + i + "</a></li>";
                    }
                }
            }
            content += "<li><a href=\"javascript:;\">下一页</a></li>";
            content += "<li><a href=\"javascript:;\">尾页</a></li>";
            // content += "<span> 共<span>"+total+"</span>页 </span>";
            // content += "<span class='totalSize'> 共<span>"+totalNum+"</span>条记录 </span>";



            content += "</ul>";
            content += "<small class=\"text-muted inline m-t-xs m-b-sm\" style=\"float:right;padding-right:10px;\">共"+totalNum+"条数据</small><div class='visible-xs'><br/></div>";


            // content += "</li>";
            if(total == 0){
                content = "";
            }
            me.element.html(content);
        },
        //添加页面操作事件
        bindEvent: function() {
            var me = this;
            me.element.off('click', 'a');
            me.element.on('click', 'a', function() {
                var num = $(this).html();
                // var id=$(this).attr("id");
                if(num == "上一页") {
                    if(me.options.pageNo == 1) {
                        me.options.pageNo = 1;
                    } else {
                        me.options.pageNo = +me.options.pageNo - 1;
                    }
                } else if(num == "下一页") {
                    if(me.options.pageNo == me.options.totalPage) {
                        me.options.pageNo = me.options.totalPage
                    } else {
                        me.options.pageNo = +me.options.pageNo + 1;
                    }

                } else if(num =="首页") {
                    me.options.pageNo = 1;
                } else if(num =="尾页") {
                    me.options.pageNo = me.options.totalPage;
                }else{
                    me.options.pageNo = +num;
                }
                me.creatHtml($("#paging_js_page_size").val());
                if(me.options.callback) {
                    me.options.callback(me.options.pageNo);
                }
            });
            $("#paging_js_page_size").change(function(){
                me.options.pageNo = 1;
                me.creatHtml($(this).val());
                if(me.options.callback) {
                    me.options.callback(me.options.pageNo);
                }
            })
        }
    };
    //通过jQuery对象初始化分页对象
    $.fn.paging = function(options) {
        return new Paging($(this), options);
    }
})(jQuery, window, document);